package dflow.presentation.controller;

import dflow.application.service.AuthorService;
import dflow.domain.CallResult;
import dflow.util.mail.MailContentTypeEnum;
import dflow.util.mail.MailSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;

/**
 * Created by dustin on 2017/7/18.
 */
@Controller
@RequestMapping("/user")
public class LoginController {
    @Autowired
    AuthorService authorService;

    @RequestMapping(value = "login")
    public String login(){
        return "login";
    }

    @RequestMapping(value = "loginP",method = RequestMethod.POST, produces="application/json;charset=UTF-8")
    @ResponseBody
    public CallResult login(Model model, String name, String pwd, HttpServletRequest request)throws Exception{
        CallResult res= authorService.Login(name,pwd);
        if(res.isOk()){
//            new MailSender()
//                    .title("测试SpringBoot发送邮件")
//                    .content("简单文本内容发送")
//                    .contentType(MailContentTypeEnum.TEXT)
//                    .targets(new ArrayList<String>(){{
//                        add("573217098@qq.com");
//                    }})
//                    .send();
            request.getSession().setAttribute("user",res.getData());
        }

        return res;
    }

    @RequestMapping(value = "logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        return "redirect:login";
    }

}
